//
//  RightView.m
//  Daybook
//
//  Created by dllo on 16/5/18.
//  Copyright © 2016年 zhangbin. All rights reserved.
//

#import "RightView.h"
#import "Common.h"
#import "RightViewCell.h"

@interface RightView ()
<
DatePickerViewDelegate,
ChartViewDelegate,
UITableViewDataSource,
UITableViewDelegate
>

@property (nonatomic, strong) BaseView *navigationView;
@property (nonatomic, strong) UIButton *topLeftButton;
@property (nonatomic, strong) UIButton *topMiddleButton;
@property (nonatomic, strong) UIButton *topRightButton;
@property (nonatomic, strong) UIButton *toButton;
@property (nonatomic, strong) UIButton *beginDateButton;
@property (nonatomic, strong) UIButton *endDateButton;
@property (nonatomic, strong) UIButton *leftImageButton;
@property (nonatomic, strong) UIButton *rightImageButton;

@property (nonatomic, strong) DatePickerView *datePickerView;

@property (nonatomic, strong) NSMutableDictionary *datePickerDic;

@property (nonatomic, strong) ChartView *chartView;

@property (nonatomic, strong)UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *bottomArr;

@property (nonatomic, strong) NSDate *beginDate;
@property (nonatomic, strong) NSDate *endDate;

@property (nonatomic, assign) BOOL isIncome;

@end
@implementation RightView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.datePickerDic = [NSMutableDictionary dictionary];
        self.bottomArr = [NSMutableArray array];
        self.isIncome = NO;
        self.beginDate = [[CalendarTool shareCalendarTool] firstDayFromDate:[NSDate date]];
        self.endDate = [[CalendarTool shareCalendarTool] lastDayFromDate:[NSDate date]];
        [self createTopView];
        [self createView];
        [self createData];
        [self createTableView];
    }
    return self;
}

- (void)createTopView {
    self.navigationView = [[BaseView alloc] initWithFrame:CGRectMake(0, 80 * MAIN_HEIGHT / 736.0 - 1, MAIN_WIDTH, 1)];
    [self addSubview:_navigationView];
    self.navigationView.backgroundColor = LINE_COLOR;
    
    self.topLeftButton = ({
        UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
        tempButton.frame = CGRectMake((10 / 414.0 * MAIN_WIDTH), MAIN_HEIGHT * (35 / 736.0), 60 / 414.0 * MAIN_WIDTH, 30 / 414.0 * MAIN_WIDTH);
        [self addSubview:tempButton];
        [tempButton setTitle:@"收入" forState:UIControlStateNormal];
        [tempButton addTarget:self action:@selector(topLeftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [tempButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        tempButton.backgroundColor = [UIColor lightGrayColor];
//        tempButton.layer.borderWidth = 1;
//        tempButton.layer.borderColor = [[UIColor lightGrayColor]CGColor];
        tempButton;
    });
    
    self.topMiddleButton = ({
        UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
        tempButton.frame = CGRectMake(0, 0, 80 / 414.0 * MAIN_WIDTH, 30 / 414.0 * MAIN_WIDTH);
        tempButton.center = CGPointMake(MAIN_WIDTH / 2, (MAIN_HEIGHT * (50 / 736.0)));
        [self addSubview:tempButton];
        [tempButton setTitle:@"图表" forState:UIControlStateNormal];
        [tempButton addTarget:self action:@selector(topMiddleButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [tempButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        tempButton.backgroundColor = [UIColor lightGrayColor];
//        tempButton.layer.borderWidth = 1;
//        tempButton.layer.borderColor = [[UIColor lightGrayColor]CGColor];
        tempButton;
    });
    
    self.topRightButton = ({
        UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
        tempButton.frame = CGRectMake((MAIN_WIDTH - 70 / 414.0 * MAIN_WIDTH), (MAIN_HEIGHT * (35 / 736.0)), 60 / 414.0 * MAIN_WIDTH, 30 / 414.0 * MAIN_WIDTH);
        [self addSubview:tempButton];
        [tempButton setTitle:@"支出" forState:UIControlStateNormal];
        [tempButton addTarget:self action:@selector(topRightButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [tempButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        tempButton;
    });
    
    
    self.beginDateButton = ({
        UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
        tempButton.frame = CGRectMake(0, 0, 60 / 414.0 * MAIN_WIDTH, 30 / 414.0 * MAIN_WIDTH);
        tempButton.center = CGPointMake(MAIN_WIDTH / 2 - 45 / 414.0 * MAIN_WIDTH, (MAIN_HEIGHT * (100 / 736.0)));
        [self addSubview:tempButton];
        NSString *firstStr = [NSString stringWithFormat:@"%02ld-01", [[CalendarTool shareCalendarTool] month:[NSDate date]]];
        [tempButton setTitle:firstStr forState:UIControlStateNormal];
        [tempButton addTarget:self action:@selector(popDateViewAction:) forControlEvents:UIControlEventTouchUpInside];
        [tempButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        tempButton.titleLabel.font = [UIFont systemFontOfSize:16];
        tempButton;
    });
    
    self.endDateButton = ({
        UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
        tempButton.frame = CGRectMake(0, 0, 60 / 414.0 * MAIN_WIDTH, 30 / 414.0 * MAIN_WIDTH);
        tempButton.center = CGPointMake(MAIN_WIDTH / 2  + 45 / 414.0 * MAIN_WIDTH, (MAIN_HEIGHT * (100 / 736.0)));
        [self addSubview:tempButton];
        NSString *lastStr = [NSString stringWithFormat:@"%02ld-%02ld", [[CalendarTool shareCalendarTool] month:[NSDate date]], [[CalendarTool shareCalendarTool] totaldaysInMonth:[NSDate date]]];
        [tempButton setTitle:lastStr forState:UIControlStateNormal];
        [tempButton addTarget:self action:@selector(popDateViewAction:) forControlEvents:UIControlEventTouchUpInside];
        [tempButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        tempButton.titleLabel.font = [UIFont systemFontOfSize:16];
        //        tempButton.backgroundColor = [UIColor lightGrayColor];
        //        tempButton.layer.borderWidth = 1;
        //        tempButton.layer.borderColor = [[UIColor lightGrayColor]CGColor];
        tempButton;
    });
    
    self.toButton = ({
        UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
        tempButton.frame = CGRectMake(0, 0, 60 / 414.0 * MAIN_WIDTH, 30 / 414.0 * MAIN_WIDTH);
        tempButton.center = CGPointMake(MAIN_WIDTH / 2, (MAIN_HEIGHT * (100 / 736.0)));
        [self addSubview:tempButton];
        [tempButton setTitle:@"至" forState:UIControlStateNormal];
        [tempButton addTarget:self action:@selector(popDateViewAction:) forControlEvents:UIControlEventTouchUpInside];
        [tempButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        //        tempButton.backgroundColor = [UIColor lightGrayColor];
        //        tempButton.layer.borderWidth = 1;
        //        tempButton.layer.borderColor = [[UIColor lightGrayColor]CGColor];
        tempButton;
    });
    
    self.leftImageButton = ({
        UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
        tempButton.frame = CGRectMake(0, 0, 15 / 414.0 * MAIN_WIDTH, 15 / 414.0 * MAIN_WIDTH);
        tempButton.center = CGPointMake(MAIN_WIDTH / 2  - 95 / 414.0 * MAIN_WIDTH, (MAIN_HEIGHT * (100 / 736.0)));
        [self addSubview:tempButton];
        [tempButton setImage:[UIImage imageNamed:@"fanhui.png"] forState:UIControlStateNormal];
        [tempButton addTarget:self action:@selector(popDateViewAction:) forControlEvents:UIControlEventTouchUpInside];
        [tempButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        tempButton;
    });
    
    self.rightImageButton = ({
        UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
        tempButton.frame = CGRectMake(0, 0, 15 / 414.0 * MAIN_WIDTH, 15 / 414.0 * MAIN_WIDTH);
        tempButton.center = CGPointMake(MAIN_WIDTH / 2  + 90 / 414.0 * MAIN_WIDTH, (MAIN_HEIGHT * (100 / 736.0)));
        [self addSubview:tempButton];
        [tempButton setImage:[UIImage imageNamed:@"xiangqian.png"] forState:UIControlStateNormal];
        [tempButton addTarget:self action:@selector(popDateViewAction:) forControlEvents:UIControlEventTouchUpInside];
        [tempButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        tempButton;
    });
    
}

- (void)createView {
    self.chartView = [[ChartView alloc] initWithFrame:CGRectMake(0, [CalculateFrameView setHeight:130], MAIN_WIDTH, [CalculateFrameView setHeight:360])];
    [self addSubview:_chartView];
    _chartView.delegate = self;
    
    self.backgroundColor = [UIColor whiteColor];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(rightViewExchangeAccountBook:) name:@"rightViewExchangeAccountBook" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(introduceChange) name:@"introduceChange" object:nil];

}


- (void)topLeftButtonAction:(UIButton *)button {
    [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [self.topRightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    // 图表变化
    _chartView.inOut = NO;
    [_chartView refreshData];
    // tableView变化
    _isIncome = YES;
    [self createData];
    [_tableView reloadData];
}

- (void)topMiddleButtonAction:(UIButton *)button {
    
}

- (void)topRightButtonAction:(UIButton *)button {
    [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [self.topLeftButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    // 图表变化
    _chartView.inOut = YES;
    [_chartView refreshData];
    // tableView变化
    _isIncome = NO;
    [self createData];
    [_tableView reloadData];
}

- (void)popDateViewAction:(UIButton *)button {
    [self.datePickerView show];
}

- (void)cancelButtonAction {
    [self.datePickerView hide];
}


- (void)sureButtonActionWithBeginTime:(NSString *)beginTime endTime:(NSString *)endTime {
    [self.datePickerDic setObject:beginTime forKey:@"beginTime"];
    [self.datePickerDic setObject:endTime forKey:@"endTime"];
    
    [self.beginDateButton setTitle:[beginTime substringFromIndex:5] forState:UIControlStateNormal];
    [self.endDateButton setTitle:[endTime substringFromIndex:5] forState:UIControlStateNormal];
    [self.datePickerView hide];
    

}

- (void)createData {

//    if ([[ChartTool shareChartTool] isNullInDataBaseWithBeginDate:_beginDate endDate:_endDate type: [NSString stringWithFormat:@"%d", _isIncome]]) {
//        _tableView.hidden = YES;
//    }else {
//        _tableView.hidden = NO;
        if (_isIncome) {
            self.bottomArr = [[ChartTool shareChartTool] incomeArrayWithBeginDate:_beginDate endDate:_endDate];
        }else{
            self.bottomArr = [[ChartTool shareChartTool] outLayArrayWithBeginDate:_beginDate endDate:_endDate];
        }
//    }
}

- (void)createTableView {
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, [CalculateFrameView setHeight:490], MAIN_WIDTH, MAIN_HEIGHT - [CalculateFrameView setHeight:490]) style:UITableViewStylePlain];
    _tableView.backgroundColor= [UIColor whiteColor];
    _tableView.dataSource = self;
    _tableView.delegate = self;
//    _tableView.rowHeight = 60;
    [self addSubview:_tableView];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [_tableView registerClass:[RightViewCell class] forCellReuseIdentifier:NSStringFromClass([RightViewCell class])];
    
    
    self.datePickerView = [[DatePickerView alloc] init];
    _datePickerView.delegate = self;
    _datePickerView.isGetDate = YES;
    [self addSubview:_datePickerView];
    [_datePickerView hide];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTimeAction:) name:@"changeTime" object:nil];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _bottomArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RightViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([RightViewCell class]) forIndexPath:indexPath];
    ChartModel *model = _bottomArr[indexPath.row];
    cell.nameLabel.text = model.name;
    cell.picImageView.image = [UIImage imageNamed:model.picName];
    cell.percentageLabel.text = model.proportionNum;
    cell.moneyLabel.text = model.amount;
//    cell.sele
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [CalculateFrameView setHeight:60];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];

    ChartModel *model = _bottomArr[indexPath.row];
    [self.delegate jumpToRightDetailViewWithModel:model];
}

- (void)isIncomeTure:(BOOL)ture {
    self.isIncome = ture;
    [self createData];
    [_tableView reloadData];
}

- (void)rightViewExchangeAccountBook:(NSNotification *)notification {
    [self createData];
    [_tableView reloadData];
    [_chartView refreshData];
}

- (void)changeTimeAction:(NSNotification *)notification {
    NSString *beginStr = [notification.userInfo valueForKey:@"beginTime"];
    NSString *endStr = [notification.userInfo valueForKey:@"endTime"];
    // chartView变化
    NSDate *firstDate = [[CalendarTool shareCalendarTool] dateFromString:beginStr];
    NSDate *endDate = [[CalendarTool shareCalendarTool] dateFromString:endStr];
    
    _chartView.beginDate = [[CalendarTool shareCalendarTool] beginDateFromSmallStringDate:firstDate];
    _chartView.endDate = [[CalendarTool shareCalendarTool] endDateFromSmallStringDate:endDate];
    
    // tableView变化
    self.beginDate = _chartView.beginDate;
    self.endDate = _chartView.endDate;
    [self createData];
    [_tableView reloadData];
    [_chartView refreshData];
}

- (void)introduceChange {
//    [self createData];
//    [_tableView reloadData];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshChartAction" object:nil];
//    [_chartView.pieChartView reloadChart];
//    [_chartView refreshData];
    [self createData];
    [_tableView reloadData];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"changeTime" object:nil];
}

@end
